package com.devgroupvn.guinness.youtube;

import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.devgroupvn.guinness.Application;
import com.devgroupvn.guinness.R;
import com.kin.bitmapcatching.DisplayBitmap.ObserveCatching;
import com.kin.bitmapcatching.ImageDownloader;
import com.kin.bitmapcatching.ImageDownloader.AsyncDownloadDrawable;
import com.kin.bitmapcatching.ImageDownloader.BitmapDownloaderTask;

public class ListYoutubeAdapter extends BaseAdapter implements ObserveCatching
{
	Activity mActivity;
	List<YoutubeData> mList;
	public ListYoutubeAdapter(Activity activity,List<YoutubeData> _List)
	{
		mList = _List;
		mActivity = activity;
	}
	public void setListYoutube(List<YoutubeData> _List)
	{
		mList = _List;
	}
	@Override
	public int getCount()
	{
		return mList.size();
	}

	@Override
	public Object getItem(int position)
	{
		return mList.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}
	private static class ViewHolder
	{
		ImageView img_youtubeitem;
		TextView tv_youtubeitem_title;
		TextView tv_youtubeitem_sendby;
		TextView tv_youtubeitem_countview;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		YoutubeData entry = mList.get(position);
		LayoutInflater layoutInflater = mActivity.getLayoutInflater();
		ViewHolder  holder = new ViewHolder();
		if(convertView== null)
		{
			convertView = layoutInflater.inflate(R.layout.youtube_item, null);
			holder.img_youtubeitem = (ImageView)convertView.findViewById(R.id.img_youtubeitem);
			holder.tv_youtubeitem_title = (TextView)convertView.findViewById(R.id.tv_youtubeitem_title);
			holder.tv_youtubeitem_sendby= (TextView)convertView.findViewById(R.id.tv_youtubeitem_sendby);
			holder.tv_youtubeitem_countview= (TextView)convertView.findViewById(R.id.tv_youtubeitem_countview);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_youtubeitem_title.setText(entry.getVideoName());
		holder.tv_youtubeitem_sendby.setText(entry.getUploaded());
		holder.tv_youtubeitem_countview.setText(entry.getViewCount());
		if(entry.getThumbnailLink()!=null)
		{
//			loadBitmapWithMemCache(entry.getThumbnailLink(),holder.img_youtubeitem);
		}
		
		return convertView;
	}
	
	 private void loadBitmapWithMemCache(String fileName,ImageView imageView)
	    {
	    	if (ImageDownloader.cancelPotentialWork(fileName, imageView))
	    	{
		    	final String imageKey = String.valueOf(fileName);
		    	Bitmap bitmap = getBitmapFromMemCache(imageKey);
		    	if (bitmap==null)
		    	{
		    		ImageDownloader imageDownloader = new ImageDownloader(mActivity,this);
		    		final BitmapDownloaderTask task = imageDownloader.new BitmapDownloaderTask(imageView,mActivity);
		    		Bitmap temp = BitmapFactory.decodeResource(mActivity.getResources(), R.drawable.ico_audiodefault);
		    		final AsyncDownloadDrawable asyncDrawable = new AsyncDownloadDrawable(mActivity.getResources(),temp, task);
		    		imageView.setImageDrawable(asyncDrawable);
		    		task.execute(fileName);
		    	}
		    	else
		    	{
		    		imageView.setImageBitmap(bitmap);
		    	}
	    	}
	    }
	@Override
	public void addBitmapToMemCache(String key, Bitmap bitmap)
	{
		if (key == null || bitmap == null) return;
    	if (getBitmapFromMemCache(key)==null)
    	{
    		Application.mMemCache.put(key, bitmap);
    	}
    	
    	if (!Application.mDiskCache.containsKey(key))
    	{
    		Application.mDiskCache.put(key, bitmap);	
    	}
	}
	@Override
	public Bitmap getBitmapFromMemCache(String key)
	{
		return Application.mMemCache.get(key);
	}
	@Override
	public Bitmap getBitmapFromDiskCache(String key)
	{
		return Application.mMemCache.get(key);
	}
	
}
